package com.kuaimacode.kframework.agent.controller.finance;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.services.finance.AgentAccountDetailService;
import com.kuaimacode.kframework.agent.services.finance.AgentDrawApplyService;
import com.kuaimacode.kframework.agent.services.sysparam.SysParamService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.finance.AgentAccountDetail;
import com.kuaimacode.kframework.mybatis.models.finance.AgentDrawApply;
import com.kuaimacode.kframework.mybatis.models.sysparam.SysParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author anzy
 * @date 2019/05/23
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/agentDrawApply"})
public class AgentDrawApplyController {
	private static String PAGE_FILE_NAME = "agentDrawApply/";

	@Autowired
	private AgentDrawApplyService agentDrawApplyServices;
	@Autowired
	private SysAgentService sysAgentService;

	@Autowired
	private SysParamService sysParamService;

	@Autowired
	private AgentAccountDetailService agentAccountDetailService;



	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						String status,ModelMap model) {
		PageInfo<AgentDrawApply> pageInfo = agentDrawApplyServices.findAll(status,pageNum, pageSize);
		// pageInfo.list agentDrawApply
		SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
		model.put("balance",agent.getBalance());
		model.put("status",status);
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		AgentDrawApply agentDrawApply = new AgentDrawApply();
		SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
		map.put("balance",agent.getBalance());
		SysParam sysParam = sysParamService.findByName(Constant.DRAW_RATE_PARAM_NAME,Constant.DEFAULT_AGENT_ID);
		map.put("draw_rate",sysParam.getValue());

		//查询分站提现账号
		agentDrawApply.setAccount(agent.getBankCard());
		agentDrawApply.setAccountName(agent.getCardName());
		agentDrawApply.setOpenBank(agent.getBankName());

		map.put("agentDrawApply", agentDrawApply);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		AgentDrawApply agentDrawApply = agentDrawApplyServices.getById(id);
		map.put("agentDrawApply", agentDrawApply);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(AgentDrawApply agentDrawApply, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				agentDrawApplyServices.saveDrawApply(agentDrawApply);
			}else{
				// 编辑保存
				agentDrawApplyServices.updateById(agentDrawApply);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		AgentDrawApply agentDrawApply = agentDrawApplyServices.getById(id);
		map.put("agentDrawApply", agentDrawApply);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			agentDrawApplyServices.cancleApply(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
